<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Notices;
use app\model\NoticesUsers;
use app\model\Users;
use think\Model;

class Notice extends BaseController
{
    public function index()
    {

        return $this->success(Notices::order('id','desc')->paginate(10));
    }


    public function create()
    {
       $notice= Notices::create(request()->param());
       $user=Users::select();
       $notices=[];
        foreach($user as $item){
            $u['users_id']=$item->id;
            $u['notice_id']=$notice->id;
            $u['created_at']=time();
            $notices[]=$u;
        }
        $noticesUsers=  new NoticesUsers();
        $noticesUsers->saveAll($notices);
        return $this->success();
    }


    public function update($id)
    {

        return $this->success(Notices::where('id',$id)->update(request()->param()));
    }


    public function delete($id)
    {
        Notices::where('id',$id)->delete();
        NoticesUsers::where('notice_id',$id)->delete();
        return $this->success();
    }
}
